package com.jdrx.sw.sewage.service;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.jdrx.sw.sewage.beans.dto.ForecastInfoQueryDTO;
import com.jdrx.sw.sewage.beans.dto.ForecastQueryDTO;
import com.jdrx.sw.sewage.beans.dto.PromptlyHandleDTO;
import com.jdrx.sw.sewage.beans.entity.ForecastPO;
import com.jdrx.sw.sewage.beans.entity.ForecastPvSPO;
import com.jdrx.sw.sewage.beans.vo.FProcessunitVO;
import com.jdrx.sw.sewage.beans.vo.ForecastInfoVO;
import com.jdrx.sw.sewage.beans.vo.ForecastVO;
import com.jdrx.sw.sewage.beans.vo.base.PageVO;
import com.jdrx.sw.sewage.common.enums.EForecastStatus;
import com.jdrx.sw.sewage.common.enums.EForecastType;
import com.jdrx.sw.sewage.dao.ForecastDAO;
import com.jdrx.sw.sewage.dao.ForecastPvDAO;
import com.jdrx.sw.sewage.dao.ForecastPvSDAO;
import com.jdrx.sw.sewage.service.base.BaseServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ForecastServiceImpl extends BaseServiceImpl {

    public static final Logger logger = LoggerFactory.getLogger(DeviceTimeDataServiceImpl.class);

    @Autowired
    private ForecastDAO forecastDAO;
    @Autowired
    private ForecastPvSDAO forecastPvSDAO;
    @Autowired
    private ForecastPvDAO forecastPvDAO;

    public PageVO<ForecastVO> selectForecastByPage(ForecastQueryDTO forecastQueryDTO) {
        PageHelper.startPage(forecastQueryDTO);
        return PageVO.toPageVO(forecastDAO.selectForecastByPage(forecastQueryDTO));
    }

    public ForecastInfoVO selectForecastInfo(ForecastInfoQueryDTO dto) {
        List<ForecastPvSPO> forecastPvSPOList = forecastPvSDAO.selectByMonitorType(dto.getMonitorType());
        ForecastInfoVO forecastInfoVO = new ForecastInfoVO();
        ArrayList<FProcessunitVO> fProcessunitVOs = Lists.newArrayList();
        forecastPvSPOList.forEach(item -> {
            FProcessunitVO fProcessunitVO = new FProcessunitVO();
            if (item.getProcessunitV().equals("TJC")) {
                fProcessunitVO.setProcessunitName("调节池");
                fProcessunitVO.setDeviceName("液位计");
                fProcessunitVO.setUnit("m");
            }
            if (item.getProcessunitV().equals("JSK")) {
                fProcessunitVO.setProcessunitName("厂站进水口");
                fProcessunitVO.setDeviceName("流量计");
                fProcessunitVO.setUnit("m³/h");
            }
            fProcessunitVO.setVal(forecastPvDAO.selectVal(item.getProcessunitV(), item.getGatherDevPointV(), dto.getStationId()));
            fProcessunitVOs.add(fProcessunitVO);
        });
        forecastInfoVO.setProcessunitS(fProcessunitVOs);

        forecastInfoVO.setForecastPvPOs(forecastPvDAO.selectByForecastIdAndType(dto.getForecastId(), EForecastType.CHECK.getKey()));
        return forecastInfoVO;
    }

    public void promptlyHandle(PromptlyHandleDTO promptlyHandleDTO) {
        ForecastPO forecastPO = forecastDAO.selectByPrimaryKey(promptlyHandleDTO.getForecastId());
        forecastPO.setId(null);
        forecastPO.setHandleStatus(EForecastStatus.UNTREATED.getKey());
        forecastPO.setType(EForecastType.CHECK.getKey());
        forecastPO.setStartTime(LocalDateTime.now());
        forecastDAO.insert(forecastPO);
    }
}
